package com.xmin.lecture.memory;

import com.xmin.lecture.service.Assistant;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/memory")
@RequiredArgsConstructor
@RestController
public class MemoryApi {
    final ChatLanguageModel chatLanguageModel;

    private final ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
    @GetMapping("/low/chat")
    public String lowChat(@RequestParam(value = "message") String message) {

        chatMemory.add(UserMessage.userMessage(message));
        ChatResponse chat = chatLanguageModel.chat(chatMemory.messages());
        chatMemory.add(chat.aiMessage());
        return chat.aiMessage().text();

    }

    final Assistant assistant;

    @GetMapping("/high/chat")
    public String highChat( @RequestParam(value = "memoryId") String memoryId, @RequestParam(value = "message") String message) {

        return assistant.chat(memoryId, message);

    }
}
